package com.itlong.cloud.commons.service.impl;

import com.itlong.cloud.POJO.DTO.operate.UploadImageDTO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.commons.service.IUploadImageService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.file.UploadFileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

/**
 * <desc>
 *      上传图片到图片服务器公共模块接口实现。
 * <desc/>
 *
 * @createDate 2017/10/17.
 */
@Service
@RefreshScope
public class UploadImageServiceImpl implements IUploadImageService{


    @Value("${" + PlatformConstants.FILE_FTP_HOST + "}")
    public String FILE_FTP_HOST;

    @Value("${" + PlatformConstants.FILE_FTP_PORT + "}")
    public String FILE_FTP_PORT;

    @Value("${" + PlatformConstants.FILE_FTP_USERNAME + "}")
    public String FILE_FTP_USERNAME;

    @Value("${" + PlatformConstants.FILE_FTP_PASSWORD + "}")
    public String FILE_FTP_PASSWORD;

    @Value("${" + PlatformConstants.FILE_ACCESS_URL + "}")
    public String FILE_ACCESS_URL;

    /**
     * <desc>
     *      上传图片。
     * <desc/>
     *
     * @param
     * @return
     * @author shengen.T
     * @createDate 2017/10/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String uploadImage(UploadImageDTO saveDTO) throws Exception{
        String imgUrl ="";
        try{
            // 图像数据为空
            if (saveDTO.getImgBase64() == null){
                return imgUrl;
            }
            BASE64Decoder decoder = new BASE64Decoder();
            // Base64解码
            byte[] b = decoder.decodeBuffer(saveDTO.getImgBase64());
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            InputStream inputStream = new ByteArrayInputStream(b);
            UploadFileUtil.UploadFile uploadFileUtil = new UploadFileUtil().new UploadFile(
                    FILE_FTP_HOST,
                    FILE_FTP_USERNAME,
                    FILE_FTP_PASSWORD,
                    FILE_FTP_PORT,
                    inputStream,
                    saveDTO.getImgType(),
                    FILE_ACCESS_URL,
                    saveDTO.getFileCatalogName()
            );
            long startTime = System.currentTimeMillis(); //程序开始记录时间
            Map<String, Object> resultMap = UploadFileUtil.uploadFile(uploadFileUtil);
            long endTime   = System.currentTimeMillis(); //程序结束记录时间
            long TotalTime = endTime - startTime;       //总消耗时间
            System.out.print(String.format("上传一张图片耗时：%s 毫秒\t\n",TotalTime));
            if ("true".equals(resultMap.get("returnCode").toString())){
                imgUrl = resultMap.get("accessPath").toString();
            }
        }catch (Exception e){
            throw new DataAccessException("【上传图片uploadImage】上传图片资源失败",e);
        }
        return imgUrl;
    }
}
